﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using CDStore3Tiers;

namespace CuaHangDiaNhac
{
    public partial class frmLogin : DevComponents.DotNetBar.Metro.MetroForm
    {
        log4net.ILog log = log4net.LogManager.GetLogger("Đăng nhập");
        public static int qh;
        public static string ten;
        public static string pass;
        public static string id;
        XLDangNhap XL = new XLDangNhap();
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            txtUser.Focus();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public static bool isLogin;
        private void buttonX2_Click(object sender, EventArgs e)
        {
            try
            {
                isLogin = false;
                if (XL.login(txtUser.Text, txtPass.Text))
                {
                    isLogin = true;
                    ten = XL.GetName(txtUser.Text);
                    id = txtUser.Text;
                    pass = txtPass.Text;
                    qh = XL.GetQuyenHan(txtUser.Text);
                    log.Info(id + ": đã đăng nhập thành công");
                    this.Close();
                    
                }
                else
                {
                    txtPass.Clear();
                    throw new Exception("Đăng nhập không thành công !"); 
                }
                    
            }
            catch (Exception ex)
        {
                DevComponents.DotNetBar.MessageBoxEx.Show(ex.Message, "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void logout()
        {
            isLogin = false;
        }

        private void frmLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonX2.PerformClick();
            }
        }

        private void txtPass_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonX2.PerformClick();
            }
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                buttonX2.PerformClick();
            }
        }

    }
}